﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Productions;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace MangoMes.Write.API.Applications.Strippings
{
    public class StrippingUpdateHandler : IRequestHandler<StrippingUpdateCommand, ResultsDTO>
    {
        #region 仓储
        private readonly IBateRepository<Stripping> StrippingRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="StrippingRepository"></param>
        public StrippingUpdateHandler(IBateRepository<Stripping> StrippingRepository)
        {
            this.StrippingRepository = StrippingRepository;
        }
        #endregion

        #region 生产退料 修改
        /// <summary>
        ///  生产退料 修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultsDTO> Handle(StrippingUpdateCommand request, CancellationToken cancellationToken)
        {
            //异常处理
            try
            {
                //初始化一个ResultDTO对象result，并设置Code为 0
                ResultsDTO result = new ResultsDTO() { Code = 0 };
                //创建DapperHelper对象 用于与数据库进行交互
                DapperHelper helper = new DapperHelper();

                Stripping stripping = new Stripping();

                //连接mySql 
                string conn = "server=112.124.69.237;port=3306;uid=root;pwd=No6123666;database=MangoMesDB";

                var str = "UPDATE stripping SET StrippingNode='" + request.StrippingNode + "',StrippingName='" + request.StrippingName + "',ProductionWorkOrdersNo='" + request.ProductionWorkOrdersNo + "',StashId='" + request.StashId + "',StrippingTime='" + request.StrippingTime + "',Remark='" + request.Remark + "',Documents='" + request.Documents + "'WHERE StrippingId=" + request.StrippingId + "";

                var getnum = StrippingRepository.Add(str, stripping, conn);
                if (getnum != null)
                {
                    result.Code = 1; //如果getnum!=null，表示修改成功
                }
                result.Message = "生产退料 修改成功";

                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

    }
}
